package com.erp.controller.baseDataController;

import com.erp.pojo.Person;
import com.erp.service.baseDataService.PersonService;
import com.erp.utils.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * Created by IntelliJ IDEA.
 * Author:${JayC}
 * Date:2019/1/19
 * Time:10:25
 */
@RestController
@RequestMapping("/Person")
public class PersonController {

    @Autowired
    private PersonService personService;
    //查询
    @PostMapping("getPerson")
    public Msg getPerson(@RequestParam(name = "name",defaultValue = "")String name,
                        @RequestParam(name = "type",defaultValue = "")String type,
                        @RequestParam(name = "page")int page,
                        @RequestParam(name = "limit")int limit){

        Msg msg = personService.getPerson(name, type, page, limit);

        return msg;

    }

    /**
     *
     * @param id
     * @return
     */
    @PostMapping("getPersonById")
    public Person getPersonById(@RequestParam(name = "id",defaultValue = "")Long id){

        Person person = personService.getPersonById(id);

        return person;

    }

    //删除
    @DeleteMapping("/delPerson/{id}")
    public void deletePerson(@PathVariable(name = "id")Long id){
        personService.deletePerson(id);
    }

    //多条删除
    @DeleteMapping("/delPersons/{ids}")
    public void delPersons(@PathVariable(name = "ids")String ids){
        String a[] = ids.split(",");
        System.out.println(Arrays.toString(a));

        for (int i = 0; i < a.length; i++) {
            Long id = Long.valueOf(a[i]);
            personService.deletePerson(id);

            System.out.printf("进入删除控制器");
        }
    }

    //增加
    @PostMapping("/insertPerson")
    public void insertInout(Person person){
        personService.insertPerson(person);
    }

    //修改
    @PostMapping("updaPerson")
    public void updatePerson(Person person){

         personService.updataPerson(person);


    }
}
